package com.dotflat.xj.controller;

import com.dotflat.rest.controller.RestfulBaseController;
import com.dotflat.xj.domain.Data;
import com.dotflat.xj.domain.TargetClass;
import com.dotflat.xj.po.TargetClassPO;
import com.dotflat.xj.service.TargetClassService;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.domain.cookbook.Recipe;
import com.matt.domain.cookbook.RecipeConfig;
import com.matt.domain.cookbook.RecipeFood;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.utils.CookbookUtils;
import com.matt.foundation.utils.ExcelOperateUtil;
import com.matt.service.cookbook.RecipeConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.assertj.core.util.Arrays;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.*;

import static com.matt.foundation.constant.RequestConstant.TICKET;


/**
 * 巡检班级Controller
 *
 * @author #
 * @date 2021-11-25
 */
@RestController
@RequestMapping("/api/xj_target_class")
@Slf4j
@Api(tags = "巡检 - 班级 - 卡")
public class XJTargetClassController extends RestfulBaseController<TargetClass, TargetClassService, TargetClassPO>
{


    @Autowired
    public void setBaseService(TargetClassService service) {
        super.setBaseService(service);
    }


    @Autowired
    private  TargetClassService targetClassService;

    @Autowired
    protected MemVerificationCache cache;

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "班级id", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "类型", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "名称", required = true, dataType = "string")

    })
    public StatusResponse update(@RequestHeader(TICKET) String ticket, String id,String type, String name){
        StatusResponse statusResponse = get(ticket,id);
        TargetClass targetClass = (TargetClass) statusResponse.getData();
        targetClass.setType(type);
        targetClass.setName(name);
        targetClassService.update(targetClass);
        return StatusResponse.success("修改成功");
    }

    @ApiOperation(value = "巡检 - 绑卡")
    @PostMapping("/cardCode")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "巡检对象id", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "cardCode", value = "卡码", required = true, dataType = "string"),
    })
    public StatusResponse cardCode(@RequestHeader(TICKET) String ticket, String id,String cardCode){
        StaffDTO staffDTO = cache.getStaff(ticket);
        if (Objects.isNull(staffDTO)){
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        TargetClass targetClass = targetClassService.update(  id, cardCode);
        return StatusResponse.success(targetClass);
    }

    @ApiOperation(value = "巡检 - 解绑")
    @PostMapping("/cardCodeUnbound")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "cardCode", value = "卡码", required = true, dataType = "string"),
    })
    public StatusResponse cardCodeUnbound(@RequestHeader(TICKET) String ticket, String cardCode){
        StaffDTO staffDTO = cache.getStaff(ticket);
        if (Objects.isNull(staffDTO)){
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        targetClassService.cardCodeUnbound(cardCode);
        return StatusResponse.success("解绑成功");
    }

    @ApiOperation(value = "拷贝 - 常见巡检目标")
    @PostMapping("/copyCommon")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
    })
    public StatusResponse copyCommon(@RequestHeader(TICKET) String ticket){
        StaffDTO staffDTO = cache.getStaff(ticket);
        if (Objects.isNull(staffDTO)){
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        targetClassService.copyCommon(staffDTO);
        return StatusResponse.success("成功");
    }


}